<template>
    <div class="main">
      <template>
        <el-table
          :data="suppliers"
          style="width: 100%"
          :row-class-name="tableRowClassName">
          <el-table-column
            prop="ano"
            label="用户编号"
            width="100">
          </el-table-column>
          <el-table-column
            prop="suimg"
            label="供应商照片"
            width="100"
            >
            <template slot-scope="scope">
              <img :src="getImgUrl(scope.row.suimg)" style="width: 30px;height: 30px;"/>
            </template>
          </el-table-column>
          <el-table-column
            prop="supic"
            label="供应商蔬菜详情照片"
            width="150"
          >
            <template slot-scope="scope">
              <img :src="getsupicUrl(scope.row.supic)" style="width: 30px;height: 30px;"/>
            </template>
          </el-table-column>
          <el-table-column
            prop="sid"
            label="ID"
            width="100">
          </el-table-column>
          <el-table-column
            prop="suname"
            label="供应商名称"
            width="100">
          </el-table-column>
          <el-table-column
            label="电话"
            prop="suphone"
            width="130">
          </el-table-column>
          <el-table-column
            label="地址"
            prop="suaddr"
            width="150">
          </el-table-column>
          <el-table-column
            prop="suemail"
            label="邮箱"
            width="90">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEditSupplier(scope.$index, scope.row)">修改</el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDeleteSupplier(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage"
            :page-size="pageInfo.pageSize"
            layout="prev, pager, next, jumper"
            :total="pageInfo.itemCount">
          </el-pagination>
        </div>
        <!--弹框-->
        <el-button size="mini"
                   @click="handleAddSupplier">新增</el-button>
        <addSupplier v-if="addDialogIsShow" ref="showAddSupplier"></addSupplier>
        <editSupplier v-if="editDialogIsShow" ref="showEditSupplier"></editSupplier>
        <deleteSupplier v-if="deleteDialogIsShow" ref="showDeleteSupplier"></deleteSupplier>
      </template>
    </div>
</template>

<script>
  import api from "../../../../axios/api"
  import addSupplier from '@/components/back/supplier/addSupplier.vue'
  import editSupplier from '@/components/back/supplier/editSupplier.vue'
  import deleteSupplier from '@/components/back/supplier/deleteSuppplier.vue'
    export default {
        name: "index",
      components : {
        addSupplier,
        editSupplier,
        deleteSupplier

      },
      data(){
        return {suppliers:[],
          addDialogIsShow: false,
          editDialogIsShow:false,
          deleteDialogIsShow:false,
          currentPage: 1,
          pageInfo:{},
          pics:[]
        }
      },
      methods:{
        handleSizeChange(val) {
          console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
          console.log(`当前页: ${val}`);
          this.currentPage = val;
          this.setDataFromAxios(val);
        },
        getImgUrl:function(suimg){
          return this.$store.state.host + "/upload/images/supplier/" +suimg;
        },
        getsupicUrl:function(supic){
          return this.$store.state.host + "/upload/images/supplier/" +supic;
        },
        tableRowClassName({row, rowIndex}) {
          if (rowIndex % 2==0) {
            return 'warning-row';
          } else {
            return '';
          }
          return '';
        },
      setDataFromAxios:function(currPage){
        if(currPage!=null){
          this.currentPage = currPage;
        }
        //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
        api.setDataFromAxios(this.$store.state.host+"supplierController/getSupplierByPage?currPage="+this.currentPage+"&pageSize=5",null,"post")
          .then(res=>{
            console.log(res);
            this.suppliers = res.tList
            this.pageInfo = res.obj;
          });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
      },
        handleAddSupplier:function() {
          let _this = this;
          _this.addDialogIsShow = true;
          _this.$nextTick(() => {
            _this.$refs.showAddSupplier.init();
          })
        },
        handleDeleteSupplier:function(index,supplier) {
          let _this = this;
          _this.deleteDialogIsShow = true;
          _this.$nextTick(() => {
            _this.$refs.showDeleteSupplier.init(supplier);
          })
        },
        handleEditSupplier:function(index,supplier) {
          let _this = this;
          _this.editDialogIsShow = true;
          _this.$nextTick(() => {
            _this.$refs.showEditSupplier.init(supplier);
          })
        }
    },
    //vue init方法 当前页面初始化就自动执行的方法 一般用于页面初始化就读取数据
    created:function () {
      this.setDataFromAxios();//1.创建页面的时候 调用自定义的数据请求方法  而数据请求方法把得到数据赋值给了当前页面的变量
    }
    }
</script>

<style scoped lang="stylus">
.main
  width 1100px
  margin 0 auto
/deep/ .warning-row  /*不写deep渲染出错*/
  background-color oldlace
/deep/ .success-row
  background-color #f0f9eb

</style>
